home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / tcl / dist6.3 / tclBasic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-10  |  26.9 KB  |  1,038 lines

  1. /* 
  2.  * tclBasic.c --
  3.  *
  4.  *    Contains the basic facilities for TCL command interpretation,
  5.  *    including interpreter creation and deletion, command creation
  6.  *    and deletion, and command parsing and execution.
  7.  *
  8.  * Copyright 1987-1991 Regents of the University of California
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  */
  17.  
  18. #ifndef lint
  19. static char rcsid[] = "$Header: /user6/ouster/tcl/RCS/tclBasic.c,v 1.129 92/02/10 09:29:09 ouster Exp $ SPRITE (Berkeley)";
  20. #endif
  21.  
  22. #include "tclInt.h"
  23.  
  24. /*
  25.  * The following structure defines all of the commands in the Tcl core,
  26.  * and the C procedures that execute them.
  27.  */
  28.  
  29. typedef struct {
  30.     char *name;            /* Name of command. */
  31.     Tcl_CmdProc *proc;        /* Procedure that executes command. */
  32. } CmdInfo;
  33.  
  34. /*
  35.  * Built-in commands, and the procedures associated with them:
  36.  */
  37.  
  38. static CmdInfo builtInCmds[] = {
  39.     /*
  40.      * Commands in the generic core:
  41.      */
  42.  
  43.     {"append",        Tcl_AppendCmd},
  44.     {"array",        Tcl_ArrayCmd},
  45.     {"break",        Tcl_BreakCmd},
  46.     {"case",        Tcl_CaseCmd},
  47.     {"catch",        Tcl_CatchCmd},
  48.     {"concat",        Tcl_ConcatCmd},
  49.     {"continue",    Tcl_ContinueCmd},
  50.     {"error",        Tcl_ErrorCmd},
  51.     {"eval",        Tcl_EvalCmd},
  52.     {"expr",        Tcl_ExprCmd},
  53.     {"for",        Tcl_ForCmd},
  54.     {"foreach",        Tcl_ForeachCmd},
  55.     {"format",        Tcl_FormatCmd},
  56.     {"global",        Tcl_GlobalCmd},
  57.     {"if",        Tcl_IfCmd},
  58.     {"incr",        Tcl_IncrCmd},
  59.     {"info",        Tcl_InfoCmd},
  60.     {"join",        Tcl_JoinCmd},
  61.     {"lappend",        Tcl_LappendCmd},
  62.     {"lindex",        Tcl_LindexCmd},
  63.     {"linsert",        Tcl_LinsertCmd},
  64.     {"list",        Tcl_ListCmd},
  65.     {"llength",        Tcl_LlengthCmd},
  66.     {"lrange",        Tcl_LrangeCmd},
  67.     {"lreplace",    Tcl_LreplaceCmd},
  68.     {"lsearch",        Tcl_LsearchCmd},
  69.     {"lsort",        Tcl_LsortCmd},
  70.     {"proc",        Tcl_ProcCmd},
  71.     {"regexp",        Tcl_RegexpCmd},
  72.     {"regsub",        Tcl_RegsubCmd},
  73.     {"rename",        Tcl_RenameCmd},
  74.     {"return",        Tcl_ReturnCmd},
  75.     {"scan",        Tcl_ScanCmd},
  76.     {"set",        Tcl_SetCmd},
  77.     {"split",        Tcl_SplitCmd},
  78.     {"string",        Tcl_StringCmd},
  79.     {"trace",        Tcl_TraceCmd},
  80.     {"unset",        Tcl_UnsetCmd},
  81.     {"uplevel",        Tcl_UplevelCmd},
  82.     {"upvar",        Tcl_UpvarCmd},
  83.     {"while",        Tcl_WhileCmd},
  84.  
  85.     /*
  86.      * Commands in the UNIX core:
  87.      */
  88.  
  89. #ifndef TCL_GENERIC_ONLY
  90.     {"cd",        Tcl_CdCmd},
  91.     {"close",        Tcl_CloseCmd},
  92.     {"eof",        Tcl_EofCmd},
  93.     {"exec",        Tcl_ExecCmd},
  94.     {"exit",        Tcl_ExitCmd},
  95.     {"file",        Tcl_FileCmd},
  96.     {"flush",        Tcl_FlushCmd},
  97.     {"gets",        Tcl_GetsCmd},
  98.     {"glob",        Tcl_GlobCmd},
  99.     {"open",        Tcl_OpenCmd},
  100.     {"puts",        Tcl_PutsCmd},
  101.     {"pwd",        Tcl_PwdCmd},
  102.     {"read",        Tcl_ReadCmd},
  103.     {"seek",        Tcl_SeekCmd},
  104.     {"source",        Tcl_SourceCmd},
  105.     {"tell",        Tcl_TellCmd},
  106.     {"time",        Tcl_TimeCmd},
  107. #endif /* TCL_GENERIC_ONLY */
  108.     {NULL,        (Tcl_CmdProc *) NULL}
  109. };
  110.  
  111. /*
  112.  *----------------------------------------------------------------------
  113.  *
  114.  * Tcl_CreateInterp --
  115.  *
  116.  *    Create a new TCL command interpreter.
  117.  *
  118.  * Results:
  119.  *    The return value is a token for the interpreter, which may be
  120.  *    used in calls to procedures like Tcl_CreateCmd, Tcl_Eval, or
  121.  *    Tcl_DeleteInterp.
  122.  *
  123.  * Side effects:
  124.  *    The command interpreter is initialized with an empty variable
  125.  *    table and the built-in commands.
  126.  *
  127.  *----------------------------------------------------------------------
  128.  */
  129.  
  130. Tcl_Interp *
  131. Tcl_CreateInterp()
  132. {
  133.     register Interp *iPtr;
  134.     register Command *cmdPtr;
  135.     register CmdInfo *cmdInfoPtr;
  136.     int i;
  137.  
  138.     iPtr = (Interp *) ckalloc(sizeof(Interp));
  139.     iPtr->result = iPtr->resultSpace;
  140.     iPtr->freeProc = 0;
  141.     iPtr->errorLine = 0;
  142.     Tcl_InitHashTable(&iPtr->commandTable, TCL_STRING_KEYS);
  143.     Tcl_InitHashTable(&iPtr->globalTable, TCL_STRING_KEYS);
  144.     iPtr->numLevels = 0;
  145.     iPtr->framePtr = NULL;
  146.     iPtr->varFramePtr = NULL;
  147.     iPtr->activeTracePtr = NULL;
  148.     iPtr->numEvents = 0;
  149.     iPtr->events = NULL;
  150.     iPtr->curEvent = 0;
  151.     iPtr->curEventNum = 0;
  152.     iPtr->revPtr = NULL;
  153.     iPtr->historyFirst = NULL;
  154.     iPtr->revDisables = 1;
  155.     iPtr->evalFirst = iPtr->evalLast = NULL;
  156.     iPtr->appendResult = NULL;
  157.     iPtr->appendAvl = 0;
  158.     iPtr->appendUsed = 0;
  159.     iPtr->numFiles = 0;
  160.     iPtr->filePtrArray = NULL;
  161.     for (i = 0; i < NUM_REGEXPS; i++) {
  162.     iPtr->patterns[i] = NULL;
  163.     iPtr->regexps[i] = NULL;
  164.     }
  165.     iPtr->cmdCount = 0;
  166.     iPtr->noEval = 0;
  167.     iPtr->scriptFile = NULL;
  168.     iPtr->flags = 0;
  169.     iPtr->tracePtr = NULL;
  170.     iPtr->resultSpace[0] = 0;
  171.  
  172.     /*
  173.      * Create the built-in commands.  Do it here, rather than calling
  174.      * Tcl_CreateCommand, because it's faster (there's no need to
  175.      * check for a pre-existing command by the same name).
  176.      */
  177.  
  178.     for (cmdInfoPtr = builtInCmds; cmdInfoPtr->name != NULL; cmdInfoPtr++) {
  179.     int new;
  180.     Tcl_HashEntry *hPtr;
  181.  
  182.     hPtr = Tcl_CreateHashEntry(&iPtr->commandTable,
  183.         cmdInfoPtr->name, &new);
  184.     if (new) {
  185.         cmdPtr = (Command *) ckalloc(sizeof(Command));
  186.         cmdPtr->proc = cmdInfoPtr->proc;
  187.         cmdPtr->clientData = (ClientData) NULL;
  188.         cmdPtr->deleteProc = NULL;
  189.         Tcl_SetHashValue(hPtr, cmdPtr);
  190.     }
  191.     }
  192.  
  193. #ifndef TCL_GENERIC_ONLY
  194.     TclSetupEnv((Tcl_Interp *) iPtr);
  195. #endif
  196.  
  197.     return (Tcl_Interp *) iPtr;
  198. }
  199.  
  200. /*
  201.  *----------------------------------------------------------------------
  202.  *
  203.  * Tcl_DeleteInterp --
  204.  *
  205.  *    Delete an interpreter and free up all of the resources associated
  206.  *    with it.
  207.  *
  208.  * Results:
  209.  *    None.
  210.  *
  211.  * Side effects:
  212.  *    The interpreter is destroyed.  The caller should never again
  213.  *    use the interp token.
  214.  *
  215.  *----------------------------------------------------------------------
  216.  */
  217.  
  218. void
  219. Tcl_DeleteInterp(interp)
  220.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  221.                  * by a previous call to Tcl_CreateInterp). */
  222. {
  223.     Interp *iPtr = (Interp *) interp;
  224.     Tcl_HashEntry *hPtr;
  225.     Tcl_HashSearch search;
  226.     register Command *cmdPtr;
  227.     int i;
  228.  
  229.     /*
  230.      * If the interpreter is in use, delay the deletion until later.
  231.      */
  232.  
  233.     iPtr->flags |= DELETED;
  234.     if (iPtr->numLevels != 0) {
  235.     return;
  236.     }
  237.  
  238.     /*
  239.      * Free up any remaining resources associated with the
  240.      * interpreter.
  241.      */
  242.  
  243.     for (hPtr = Tcl_FirstHashEntry(&iPtr->commandTable, &search);
  244.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  245.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  246.     if (cmdPtr->deleteProc != NULL) { 
  247.         (*cmdPtr->deleteProc)(cmdPtr->clientData);
  248.     }
  249.     ckfree((char *) cmdPtr);
  250.     }
  251.     Tcl_DeleteHashTable(&iPtr->commandTable);
  252.     TclDeleteVars(iPtr, &iPtr->globalTable);
  253.     if (iPtr->events != NULL) {
  254.     int i;
  255.  
  256.     for (i = 0; i < iPtr->numEvents; i++) {
  257.         ckfree(iPtr->events[i].command);
  258.     }
  259.     ckfree((char *) iPtr->events);
  260.     }
  261.     while (iPtr->revPtr != NULL) {
  262.     HistoryRev *nextPtr = iPtr->revPtr->nextPtr;
  263.  
  264.     ckfree((char *) iPtr->revPtr);
  265.     iPtr->revPtr = nextPtr;
  266.     }
  267.     if (iPtr->appendResult != NULL) {
  268.     ckfree(iPtr->appendResult);
  269.     }
  270. #ifndef TCL_GENERIC_ONLY
  271.     if (iPtr->numFiles > 0) {
  272.     for (i = 0; i < iPtr->numFiles; i++) {
  273.         OpenFile *filePtr;
  274.     
  275.         filePtr = iPtr->filePtrArray[i];
  276.         if (filePtr == NULL) {
  277.         continue;
  278.         }
  279.         if (i >= 3) {
  280.         fclose(filePtr->f);
  281.         if (filePtr->f2 != NULL) {
  282.             fclose(filePtr->f2);
  283.         }
  284.         if (filePtr->numPids > 0) {
  285.             Tcl_DetachPids(filePtr->numPids, filePtr->pidPtr);
  286.             ckfree((char *) filePtr->pidPtr);
  287.         }
  288.         }
  289.         ckfree((char *) filePtr);
  290.     }
  291.     ckfree((char *) iPtr->filePtrArray);
  292.     }
  293. #endif
  294.     for (i = 0; i < NUM_REGEXPS; i++) {
  295.     if (iPtr->patterns[i] == NULL) {
  296.         break;
  297.     }
  298.     ckfree(iPtr->patterns[i]);
  299.     ckfree((char *) iPtr->regexps[i]);
  300.     }
  301.     while (iPtr->tracePtr != NULL) {
  302.     Trace *nextPtr = iPtr->tracePtr->nextPtr;
  303.  
  304.     ckfree((char *) iPtr->tracePtr);
  305.     iPtr->tracePtr = nextPtr;
  306.     }
  307.     ckfree((char *) iPtr);
  308. }
  309.  
  310. /*
  311.  *----------------------------------------------------------------------
  312.  *
  313.  * Tcl_CreateCommand --
  314.  *
  315.  *    Define a new command in a command table.
  316.  *
  317.  * Results:
  318.  *    None.
  319.  *
  320.  * Side effects:
  321.  *    If a command named cmdName already exists for interp, it is
  322.  *    deleted.  In the future, when cmdName is seen as the name of
  323.  *    a command by Tcl_Eval, proc will be called.  When the command
  324.  *    is deleted from the table, deleteProc will be called.  See the
  325.  *    manual entry for details on the calling sequence.
  326.  *
  327.  *----------------------------------------------------------------------
  328.  */
  329.  
  330. void
  331. Tcl_CreateCommand(interp, cmdName, proc, clientData, deleteProc)
  332.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  333.                  * by a previous call to Tcl_CreateInterp). */
  334.     char *cmdName;        /* Name of command. */
  335.     Tcl_CmdProc *proc;        /* Command procedure to associate with
  336.                  * cmdName. */
  337.     ClientData clientData;    /* Arbitrary one-word value to pass to proc. */
  338.     Tcl_CmdDeleteProc *deleteProc;
  339.                 /* If not NULL, gives a procedure to call when
  340.                  * this command is deleted. */
  341. {
  342.     Interp *iPtr = (Interp *) interp;
  343.     register Command *cmdPtr;
  344.     Tcl_HashEntry *hPtr;
  345.     int new;
  346.  
  347.     hPtr = Tcl_CreateHashEntry(&iPtr->commandTable, cmdName, &new);
  348.     if (!new) {
  349.     /*
  350.      * Command already exists:  delete the old one.
  351.      */
  352.  
  353.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  354.     if (cmdPtr->deleteProc != NULL) {
  355.         (*cmdPtr->deleteProc)(cmdPtr->clientData);
  356.     }
  357.     } else {
  358.     cmdPtr = (Command *) ckalloc(sizeof(Command));
  359.     Tcl_SetHashValue(hPtr, cmdPtr);
  360.     }
  361.     cmdPtr->proc = proc;
  362.     cmdPtr->clientData = clientData;
  363.     cmdPtr->deleteProc = deleteProc;
  364. }
  365.  
  366. /*
  367.  *----------------------------------------------------------------------
  368.  *
  369.  * Tcl_DeleteCommand --
  370.  *
  371.  *    Remove the given command from the given interpreter.
  372.  *
  373.  * Results:
  374.  *    0 is returned if the command was deleted successfully.
  375.  *    -1 is returned if there didn't exist a command by that
  376.  *    name.
  377.  *
  378.  * Side effects:
  379.  *    CmdName will no longer be recognized as a valid command for
  380.  *    interp.
  381.  *
  382.  *----------------------------------------------------------------------
  383.  */
  384.  
  385. int
  386. Tcl_DeleteCommand(interp, cmdName)
  387.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  388.                  * by a previous call to Tcl_CreateInterp). */
  389.     char *cmdName;        /* Name of command to remove. */
  390. {
  391.     Interp *iPtr = (Interp *) interp;
  392.     Tcl_HashEntry *hPtr;
  393.     Command *cmdPtr;
  394.  
  395.     hPtr = Tcl_FindHashEntry(&iPtr->commandTable, cmdName);
  396.     if (hPtr == NULL) {
  397.     return -1;
  398.     }
  399.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  400.     if (cmdPtr->deleteProc != NULL) {
  401.     (*cmdPtr->deleteProc)(cmdPtr->clientData);
  402.     }
  403.     ckfree((char *) cmdPtr);
  404.     Tcl_DeleteHashEntry(hPtr);
  405.     return 0;
  406. }
  407.  
  408. /*
  409.  *-----------------------------------------------------------------
  410.  *
  411.  * Tcl_Eval --
  412.  *
  413.  *    Parse and execute a command in the Tcl language.
  414.  *
  415.  * Results:
  416.  *    The return value is one of the return codes defined in tcl.hd
  417.  *    (such as TCL_OK), and interp->result contains a string value
  418.  *    to supplement the return code.  The value of interp->result
  419.  *    will persist only until the next call to Tcl_Eval:  copy it or
  420.  *    lose it! *TermPtr is filled in with the character just after
  421.  *    the last one that was part of the command (usually a NULL
  422.  *    character or a closing bracket).
  423.  *
  424.  * Side effects:
  425.  *    Almost certainly;  depends on the command.
  426.  *
  427.  *-----------------------------------------------------------------
  428.  */
  429.  
  430. int
  431. Tcl_Eval(interp, cmd, flags, termPtr)
  432.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  433.                  * by a previous call to Tcl_CreateInterp). */
  434.     char *cmd;            /* Pointer to TCL command to interpret. */
  435.     int flags;            /* OR-ed combination of flags like
  436.                  * TCL_BRACKET_TERM and TCL_RECORD_BOUNDS. */
  437.     char **termPtr;        /* If non-NULL, fill in the address it points
  438.                  * to with the address of the char. just after
  439.                  * the last one that was part of cmd.  See
  440.                  * the man page for details on this. */
  441. {
  442.     /*
  443.      * The storage immediately below is used to generate a copy
  444.      * of the command, after all argument substitutions.  Pv will
  445.      * contain the argv values passed to the command procedure.
  446.      */
  447.  
  448. #   define NUM_CHARS 200
  449.     char copyStorage[NUM_CHARS];
  450.     ParseValue pv;
  451.     char *oldBuffer;
  452.  
  453.     /*
  454.      * This procedure generates an (argv, argc) array for the command,
  455.      * It starts out with stack-allocated space but uses dynamically-
  456.      * allocated storage to increase it if needed.
  457.      */
  458.  
  459. #   define NUM_ARGS 10
  460.     char *(argStorage[NUM_ARGS]);
  461.     char **argv = argStorage;
  462.     int argc;
  463.     int argSize = NUM_ARGS;
  464.  
  465.     register char *src;            /* Points to current character
  466.                      * in cmd. */
  467.     char termChar;            /* Return when this character is found
  468.                      * (either ']' or '\0').  Zero means
  469.                      * that newlines terminate commands. */
  470.     int result;                /* Return value. */
  471.     register Interp *iPtr = (Interp *) interp;
  472.     Tcl_HashEntry *hPtr;
  473.     Command *cmdPtr;
  474.     char *dummy;            /* Make termPtr point here if it was
  475.                      * originally NULL. */
  476.     char *cmdStart;            /* Points to first non-blank char. in
  477.                      * command (used in calling trace
  478.                      * procedures). */
  479.     char *ellipsis = "";        /* Used in setting errorInfo variable;
  480.                      * set to "..." to indicate that not
  481.                      * all of offending command is included
  482.                      * in errorInfo.  "" means that the
  483.                      * command is all there. */
  484.     register Trace *tracePtr;
  485.  
  486.     /*
  487.      * Initialize the result to an empty string and clear out any
  488.      * error information.  This makes sure that we return an empty
  489.      * result if there are no commands in the command string.
  490.      */
  491.  
  492.     Tcl_FreeResult((Tcl_Interp *) iPtr);
  493.     iPtr->result = iPtr->resultSpace;
  494.     iPtr->resultSpace[0] = 0;
  495.     result = TCL_OK;
  496.  
  497.     /*
  498.      * Check depth of nested calls to Tcl_Eval:  if this gets too large,
  499.      * it's probably because of an infinite loop somewhere.
  500.      */
  501.  
  502.     iPtr->numLevels++;
  503.     if (iPtr->numLevels > MAX_NESTING_DEPTH) {
  504.     iPtr->numLevels--;
  505.     iPtr->result =  "too many nested calls to Tcl_Eval (infinite loop?)";
  506.     return TCL_ERROR;
  507.     }
  508.  
  509.     /*
  510.      * Initialize the area in which command copies will be assembled.
  511.      */
  512.  
  513.     pv.buffer = copyStorage;
  514.     pv.end = copyStorage + NUM_CHARS - 1;
  515.     pv.expandProc = TclExpandParseValue;
  516.     pv.clientData = (ClientData) NULL;
  517.  
  518.     src = cmd;
  519.     if (flags & TCL_BRACKET_TERM) {
  520.     termChar = ']';
  521.     } else {
  522.     termChar = 0;
  523.     }
  524.     if (termPtr == NULL) {
  525.     termPtr = &dummy;
  526.     }
  527.     *termPtr = src;
  528.     cmdStart = src;
  529.  
  530.     /*
  531.      * There can be many sub-commands (separated by semi-colons or
  532.      * newlines) in one command string.  This outer loop iterates over
  533.      * individual commands.
  534.      */
  535.  
  536.     while (*src != termChar) {
  537.     iPtr->flags &= ~(ERR_IN_PROGRESS | ERROR_CODE_SET);
  538.  
  539.     /*
  540.      * Skim off leading white space and semi-colons, and skip
  541.      * comments.
  542.      */
  543.  
  544.     while (1) {
  545.         register char c = *src;
  546.  
  547.         if ((CHAR_TYPE(c) != TCL_SPACE) && (c != ';') && (c != '\n')) {
  548.         break;
  549.         }
  550.         src += 1;
  551.     }
  552.     if (*src == '#') {
  553.         for (src++; *src != 0; src++) {
  554.         if (*src == '\n') {
  555.             src++;
  556.             break;
  557.         }
  558.         }
  559.         continue;
  560.     }
  561.     cmdStart = src;
  562.  
  563.     /*
  564.      * Parse the words of the command, generating the argc and
  565.      * argv for the command procedure.  May have to call
  566.      * TclParseWords several times, expanding the argv array
  567.      * between calls.
  568.      */
  569.  
  570.     pv.next = oldBuffer = pv.buffer;
  571.     argc = 0;
  572.     while (1) {
  573.         int newArgs, maxArgs;
  574.         char **newArgv;
  575.         int i;
  576.  
  577.         /*
  578.          * Note:  the "- 2" below guarantees that we won't use the
  579.          * last two argv slots here.  One is for a NULL pointer to
  580.          * mark the end of the list, and the other is to leave room
  581.          * for inserting the command name "unknown" as the first
  582.          * argument (see below).
  583.          */
  584.  
  585.         maxArgs = argSize - argc - 2;
  586.         result = TclParseWords((Tcl_Interp *) iPtr, src, flags,
  587.             maxArgs, termPtr, &newArgs, &argv[argc], &pv);
  588.         src = *termPtr;
  589.         if (result != TCL_OK) {
  590.         ellipsis = "...";
  591.         goto done;
  592.         }
  593.  
  594.         /*
  595.          * Careful!  Buffer space may have gotten reallocated while
  596.          * parsing words.  If this happened, be sure to update all
  597.          * of the older argv pointers to refer to the new space.
  598.          */
  599.  
  600.         if (oldBuffer != pv.buffer) {
  601.         int i;
  602.  
  603.         for (i = 0; i < argc; i++) {
  604.             argv[i] = pv.buffer + (argv[i] - oldBuffer);
  605.         }
  606.         oldBuffer = pv.buffer;
  607.         }
  608.         argc += newArgs;
  609.         if (newArgs < maxArgs) {
  610.         argv[argc] = (char *) NULL;
  611.         break;
  612.         }
  613.  
  614.         /*
  615.          * Args didn't all fit in the current array.  Make it bigger.
  616.          */
  617.  
  618.         argSize *= 2;
  619.         newArgv = (char **)
  620.             ckalloc((unsigned) argSize * sizeof(char *));
  621.         for (i = 0; i < argc; i++) {
  622.         newArgv[i] = argv[i];
  623.         }
  624.         if (argv != argStorage) {
  625.         ckfree((char *) argv);
  626.         }
  627.         argv = newArgv;
  628.     }
  629.  
  630.     /*
  631.      * If this is an empty command (or if we're just parsing
  632.      * commands without evaluating them), then just skip to the
  633.      * next command.
  634.      */
  635.  
  636.     if ((argc == 0) || iPtr->noEval) {
  637.         continue;
  638.     }
  639.     argv[argc] = NULL;
  640.  
  641.     /*
  642.      * Save information for the history module, if needed.
  643.      */
  644.  
  645.     if (flags & TCL_RECORD_BOUNDS) {
  646.         iPtr->evalFirst = cmdStart;
  647.         iPtr->evalLast = src-1;
  648.     }
  649.  
  650.     /*
  651.      * Find the procedure to execute this command.  If there isn't
  652.      * one, then see if there is a command "unknown".  If so,
  653.      * invoke it instead, passing it the words of the original
  654.      * command as arguments.
  655.      */
  656.  
  657.     hPtr = Tcl_FindHashEntry(&iPtr->commandTable, argv[0]);
  658.     if (hPtr == NULL) {
  659.         int i;
  660.  
  661.         hPtr = Tcl_FindHashEntry(&iPtr->commandTable, "unknown");
  662.         if (hPtr == NULL) {
  663.         Tcl_ResetResult(interp);
  664.         Tcl_AppendResult(interp, "invalid command name: \"",
  665.             argv[0], "\"", (char *) NULL);
  666.         result = TCL_ERROR;
  667.         goto done;
  668.         }
  669.         for (i = argc; i >= 0; i--) {
  670.         argv[i+1] = argv[i];
  671.         }
  672.         argv[0] = "unknown";
  673.         argc++;
  674.     }
  675.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  676.  
  677.     /*
  678.      * Call trace procedures, if any.
  679.      */
  680.  
  681.     for (tracePtr = iPtr->tracePtr; tracePtr != NULL;
  682.         tracePtr = tracePtr->nextPtr) {
  683.         char saved;
  684.  
  685.         if (tracePtr->level < iPtr->numLevels) {
  686.         continue;
  687.         }
  688.         saved = *src;
  689.         *src = 0;
  690.         (*tracePtr->proc)(tracePtr->clientData, interp, iPtr->numLevels,
  691.             cmdStart, cmdPtr->proc, cmdPtr->clientData, argc, argv);
  692.         *src = saved;
  693.     }
  694.  
  695.     /*
  696.      * At long last, invoke the command procedure.  Reset the
  697.      * result to its default empty value first (it could have
  698.      * gotten changed by earlier commands in the same command
  699.      * string).
  700.      */
  701.  
  702.     iPtr->cmdCount++;
  703.     Tcl_FreeResult(iPtr);
  704.     iPtr->result = iPtr->resultSpace;
  705.     iPtr->resultSpace[0] = 0;
  706.     result = (*cmdPtr->proc)(cmdPtr->clientData, interp, argc, argv);
  707.     if (result != TCL_OK) {
  708.         break;
  709.     }
  710.     }
  711.  
  712.     /*
  713.      * Free up any extra resources that were allocated.
  714.      */
  715.  
  716.     done:
  717.     if (pv.buffer != copyStorage) {
  718.     ckfree((char *) pv.buffer);
  719.     }
  720.     if (argv != argStorage) {
  721.     ckfree((char *) argv);
  722.     }
  723.     iPtr->numLevels--;
  724.     if (iPtr->numLevels == 0) {
  725.     if (result == TCL_RETURN) {
  726.         result = TCL_OK;
  727.     }
  728.     if ((result != TCL_OK) && (result != TCL_ERROR)) {
  729.         Tcl_ResetResult(interp);
  730.         if (result == TCL_BREAK) {
  731.         iPtr->result = "invoked \"break\" outside of a loop";
  732.         } else if (result == TCL_CONTINUE) {
  733.         iPtr->result = "invoked \"continue\" outside of a loop";
  734.         } else {
  735.         iPtr->result = iPtr->resultSpace;
  736.         sprintf(iPtr->resultSpace, "command returned bad code: %d",
  737.             result);
  738.         }
  739.         result = TCL_ERROR;
  740.     }
  741.     if (iPtr->flags & DELETED) {
  742.         Tcl_DeleteInterp(interp);
  743.     }
  744.     }
  745.  
  746.     /*
  747.      * If an error occurred, record information about what was being
  748.      * executed when the error occurred.
  749.      */
  750.  
  751.     if ((result == TCL_ERROR) && !(iPtr->flags & ERR_ALREADY_LOGGED)) {
  752.     int numChars;
  753.     register char *p;
  754.  
  755.     /*
  756.      * Compute the line number where the error occurred.
  757.      */
  758.  
  759.     iPtr->errorLine = 1;
  760.     for (p = cmd; p != cmdStart; p++) {
  761.         if (*p == '\n') {
  762.         iPtr->errorLine++;
  763.         }
  764.     }
  765.     for ( ; isspace(*p) || (*p == ';'); p++) {
  766.         if (*p == '\n') {
  767.         iPtr->errorLine++;
  768.         }
  769.     }
  770.  
  771.     /*
  772.      * Figure out how much of the command to print in the error
  773.      * message (up to a certain number of characters, or up to
  774.      * the first new-line).
  775.      */
  776.  
  777.     numChars = src - cmdStart;
  778.     if (numChars > (NUM_CHARS-50)) {
  779.         numChars = NUM_CHARS-50;
  780.         ellipsis = " ...";
  781.     }
  782.  
  783.     if (!(iPtr->flags & ERR_IN_PROGRESS)) {
  784.         sprintf(copyStorage, "\n    while executing\n\"%.*s%s\"",
  785.             numChars, cmdStart, ellipsis);
  786.     } else {
  787.         sprintf(copyStorage, "\n    invoked from within\n\"%.*s%s\"",
  788.             numChars, cmdStart, ellipsis);
  789.     }
  790.     Tcl_AddErrorInfo(interp, copyStorage);
  791.     iPtr->flags &= ~ERR_ALREADY_LOGGED;
  792.     } else {
  793.     iPtr->flags &= ~ERR_ALREADY_LOGGED;
  794.     }
  795.     return result;
  796. }
  797.  
  798. /*
  799.  *----------------------------------------------------------------------
  800.  *
  801.  * Tcl_CreateTrace --
  802.  *
  803.  *    Arrange for a procedure to be called to trace command execution.
  804.  *
  805.  * Results:
  806.  *    The return value is a token for the trace, which may be passed
  807.  *    to Tcl_DeleteTrace to eliminate the trace.
  808.  *
  809.  * Side effects:
  810.  *    From now on, proc will be called just before a command procedure
  811.  *    is called to execute a Tcl command.  Calls to proc will have the
  812.  *    following form:
  813.  *
  814.  *    void
  815.  *    proc(clientData, interp, level, command, cmdProc, cmdClientData,
  816.  *        argc, argv)
  817.  *        ClientData clientData;
  818.  *        Tcl_Interp *interp;
  819.  *        int level;
  820.  *        char *command;
  821.  *        int (*cmdProc)();
  822.  *        ClientData cmdClientData;
  823.  *        int argc;
  824.  *        char **argv;
  825.  *    {
  826.  *    }
  827.  *
  828.  *    The clientData and interp arguments to proc will be the same
  829.  *    as the corresponding arguments to this procedure.  Level gives
  830.  *    the nesting level of command interpretation for this interpreter
  831.  *    (0 corresponds to top level).  Command gives the ASCII text of
  832.  *    the raw command, cmdProc and cmdClientData give the procedure that
  833.  *    will be called to process the command and the ClientData value it
  834.  *    will receive, and argc and argv give the arguments to the
  835.  *    command, after any argument parsing and substitution.  Proc
  836.  *    does not return a value.
  837.  *
  838.  *----------------------------------------------------------------------
  839.  */
  840.  
  841. Tcl_Trace
  842. Tcl_CreateTrace(interp, level, proc, clientData)
  843.     Tcl_Interp *interp;        /* Interpreter in which to create the trace. */
  844.     int level;            /* Only call proc for commands at nesting level
  845.                  * <= level (1 => top level). */
  846.     Tcl_CmdTraceProc *proc;    /* Procedure to call before executing each
  847.                  * command. */
  848.     ClientData clientData;    /* Arbitrary one-word value to pass to proc. */
  849. {
  850.     register Trace *tracePtr;
  851.     register Interp *iPtr = (Interp *) interp;
  852.  
  853.     tracePtr = (Trace *) ckalloc(sizeof(Trace));
  854.     tracePtr->level = level;
  855.     tracePtr->proc = proc;
  856.     tracePtr->clientData = clientData;
  857.     tracePtr->nextPtr = iPtr->tracePtr;
  858.     iPtr->tracePtr = tracePtr;
  859.  
  860.     return (Tcl_Trace) tracePtr;
  861. }
  862.  
  863. /*
  864.  *----------------------------------------------------------------------
  865.  *
  866.  * Tcl_DeleteTrace --
  867.  *
  868.  *    Remove a trace.
  869.  *
  870.  * Results:
  871.  *    None.
  872.  *
  873.  * Side effects:
  874.  *    From now on there will be no more calls to the procedure given
  875.  *    in trace.
  876.  *
  877.  *----------------------------------------------------------------------
  878.  */
  879.  
  880. void
  881. Tcl_DeleteTrace(interp, trace)
  882.     Tcl_Interp *interp;        /* Interpreter that contains trace. */
  883.     Tcl_Trace trace;        /* Token for trace (returned previously by
  884.                  * Tcl_CreateTrace). */
  885. {
  886.     register Interp *iPtr = (Interp *) interp;
  887.     register Trace *tracePtr = (Trace *) trace;
  888.     register Trace *tracePtr2;
  889.  
  890.     if (iPtr->tracePtr == tracePtr) {
  891.     iPtr->tracePtr = tracePtr->nextPtr;
  892.     ckfree((char *) tracePtr);
  893.     } else {
  894.     for (tracePtr2 = iPtr->tracePtr; tracePtr2 != NULL;
  895.         tracePtr2 = tracePtr2->nextPtr) {
  896.         if (tracePtr2->nextPtr == tracePtr) {
  897.         tracePtr2->nextPtr = tracePtr->nextPtr;
  898.         ckfree((char *) tracePtr);
  899.         return;
  900.         }
  901.     }
  902.     }
  903. }
  904.  
  905. /*
  906.  *----------------------------------------------------------------------
  907.  *
  908.  * Tcl_AddErrorInfo --
  909.  *
  910.  *    Add information to a message being accumulated that describes
  911.  *    the current error.
  912.  *
  913.  * Results:
  914.  *    None.
  915.  *
  916.  * Side effects:
  917.  *    The contents of message are added to the "errorInfo" variable.
  918.  *    If Tcl_Eval has been called since the current value of errorInfo
  919.  *    was set, errorInfo is cleared before adding the new message.
  920.  *
  921.  *----------------------------------------------------------------------
  922.  */
  923.  
  924. void
  925. Tcl_AddErrorInfo(interp, message)
  926.     Tcl_Interp *interp;        /* Interpreter to which error information
  927.                  * pertains. */
  928.     char *message;        /* Message to record. */
  929. {
  930.     register Interp *iPtr = (Interp *) interp;
  931.  
  932.     /*
  933.      * If an error is already being logged, then the new errorInfo
  934.      * is the concatenation of the old info and the new message.
  935.      * If this is the first piece of info for the error, then the
  936.      * new errorInfo is the concatenation of the message in
  937.      * interp->result and the new message.
  938.      */
  939.  
  940.     if (!(iPtr->flags & ERR_IN_PROGRESS)) {
  941.     Tcl_SetVar2(interp, "errorInfo", (char *) NULL, interp->result,
  942.         TCL_GLOBAL_ONLY);
  943.     iPtr->flags |= ERR_IN_PROGRESS;
  944.  
  945.     /*
  946.      * If the errorCode variable wasn't set by the code that generated
  947.      * the error, set it to "NONE".
  948.      */
  949.  
  950.     if (!(iPtr->flags & ERROR_CODE_SET)) {
  951.         (void) Tcl_SetVar2(interp, "errorCode", (char *) NULL, "NONE",
  952.             TCL_GLOBAL_ONLY);
  953.     }
  954.     }
  955.     Tcl_SetVar2(interp, "errorInfo", (char *) NULL, message,
  956.         TCL_GLOBAL_ONLY|TCL_APPEND_VALUE);
  957. }
  958.  
  959. /*
  960.  *----------------------------------------------------------------------
  961.  *
  962.  * Tcl_VarEval --
  963.  *
  964.  *    Given a variable number of string arguments, concatenate them
  965.  *    all together and execute the result as a Tcl command.
  966.  *
  967.  * Results:
  968.  *    A standard Tcl return result.  An error message or other
  969.  *    result may be left in interp->result.
  970.  *
  971.  * Side effects:
  972.  *    Depends on what was done by the command.
  973.  *
  974.  *----------------------------------------------------------------------
  975.  */
  976.     /* VARARGS2 */ /* ARGSUSED */
  977. int
  978. #ifndef lint
  979. Tcl_VarEval(va_alist)
  980. #else
  981. Tcl_VarEval(interp, p, va_alist)
  982.     Tcl_Interp *interp;        /* Interpreter in which to execute command. */
  983.     char *p;            /* One or more strings to concatenate,
  984.                  * terminated with a NULL string. */
  985. #endif
  986.     va_dcl
  987. {
  988.     va_list argList;
  989. #define FIXED_SIZE 200
  990.     char fixedSpace[FIXED_SIZE+1];
  991.     int spaceAvl, spaceUsed, length;
  992.     char *string, *cmd;
  993.     Tcl_Interp *interp;
  994.     int result;
  995.  
  996.     /*
  997.      * Copy the strings one after the other into a single larger
  998.      * string.  Use stack-allocated space for small commands, but if
  999.      * the commands gets too large than call ckalloc to create the
  1000.      * space.
  1001.      */
  1002.  
  1003.     va_start(argList);
  1004.     interp = va_arg(argList, Tcl_Interp *);
  1005.     spaceAvl = FIXED_SIZE;
  1006.     spaceUsed = 0;
  1007.     cmd = fixedSpace;
  1008.     while (1) {
  1009.     string = va_arg(argList, char *);
  1010.     if (string == NULL) {
  1011.         break;
  1012.     }
  1013.     length = strlen(string);
  1014.     if ((spaceUsed + length) > spaceAvl) {
  1015.         char *new;
  1016.  
  1017.         spaceAvl = spaceUsed + length;
  1018.         spaceAvl += spaceAvl/2;
  1019.         new = ckalloc((unsigned) spaceAvl);
  1020.         memcpy((VOID *) new, (VOID *) cmd, spaceUsed);
  1021.         if (cmd != fixedSpace) {
  1022.         ckfree(cmd);
  1023.         }
  1024.         cmd = new;
  1025.     }
  1026.     strcpy(cmd + spaceUsed, string);
  1027.     spaceUsed += length;
  1028.     }
  1029.     va_end(argList);
  1030.     cmd[spaceUsed] = '\0';
  1031.  
  1032.     result = Tcl_Eval(interp, cmd, 0, (char **) NULL);
  1033.     if (cmd != fixedSpace) {
  1034.     ckfree(cmd);
  1035.     }
  1036.     return result;
  1037. }
  1038.